package com.jmz.apt.api.Provider;

import com.jmz.apt.api.Utils.MD5Util;
import com.jmz.apt.api.exception.ExpressionUndefined;
import com.jmz.apt.api.message.request.*;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * Created by fanglexue on 2019/10/29.
 */

@Getter
@Setter
public class BaseProvider {

    private String tableName;

    protected String findAll(String columns, List<Condition> conditions,Query query) {
        StringBuffer sql = new StringBuffer("SELECT " + columns +" from " + getTableName() + "  where 1=1 ");
        sql = joinSelectSql(conditions,sql);
        return  joinPageNo(conditions,query,sql);
    }

    protected  String count(List<Condition> conditions){
        StringBuffer sql = new StringBuffer("SELECT count(1) from " + getTableName() + "  where 1=1 ");
        return joinSelectSql(conditions,sql).toString();
    }

    private StringBuffer joinSelectSql(List<Condition> conditions,StringBuffer sql){
        if (conditions != null && !conditions.isEmpty()) {
            for (Condition condition : conditions) {
                String col = condition.getName().trim();
                String val = condition.getValue().trim();
                if(col.equals("password")){
                    //TODO 后续需要抽象出来
                    val = MD5Util.encode(val);
                }
                switch (condition.getLogic()) {
                    case "eq":
                        sql = sql.append(" AND " + col + "=" + "'" + val + "'");
                        break;
                    case "neq":
                        sql = sql.append(" AND " + col + "<>" + val);
                        break;
                    case "lgt":
                        sql = sql.append(" AND " + col + "<" + val);
                        break;
                    case "gt":
                        sql = sql.append(" AND " + col + ">" + val);
                        break;
                    case "like":
                        sql = sql.append(" AND " + col + " like " + "'%" + val + "%'");
                        break;
                    case "ll":
                        sql = sql.append(" AND " + col + " like " + "'%" + val + "'");
                        break;
                    case "rl":
                        sql = sql.append(" AND " + col + " like "  + "'"+ val + "%'");
                        break;
                    case "in":
                        sql = sql.append(" AND " + col + " in (" + val + " )");
                        break;
                    case "nin":
                        sql = sql.append(" AND " + col + " not in (" + val + " )");
                        break;

                    default:
                        throw new ExpressionUndefined();
                }
            }
        }
        return sql;
    }

    private String joinPageNo(List<Condition> conditions,Query query,StringBuffer sql){
        //Paging
        Integer pageNo = query.getPageno() == null ? 0 : (Integer) query.getPageno();
        Integer pageSize = query.getPagesize() == null ? 10 : (Integer) query.getPagesize();
        sql.append(" limit " + (pageNo - 1) * pageSize + ","+ pageSize);
        return sql.toString();
    }


}
